package com.lvshi0797.cms.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvshi0797.cms.dao.IProjectInfoDao;
import com.lvshi0797.cms.dto.AjaxObj;
import com.lvshi0797.cms.dto.BaseInfoDto;
import com.lvshi0797.cms.dto.MemberDto;
import com.lvshi0797.cms.dto.ProjectDto;
import com.lvshi0797.cms.dto.ResultType;
import com.lvshi0797.cms.entities.FBaseInfo;
import com.lvshi0797.cms.entities.FManager;
import com.lvshi0797.cms.entities.Pager;
import com.lvshi0797.cms.entities.SystemContext;
import com.lvshi0797.cms.helper.BussinessException;
import com.lvshi0797.cms.service.IBaseInfoService;
import com.lvshi0797.cms.service.IMemberInfoService;

@Controller
@RequestMapping("data")
public class DataController {
	@Autowired
	private IBaseInfoService baseInfoService;
	@Autowired
	private IMemberInfoService memberInfoService;

	/**
	 * 添加或更新baseId下的所有項目信息
	 * 
	 * @param pdo
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addupdateproject", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj addProject(ProjectDto pdo, HttpSession session,
			HttpServletRequest request) {
		String niandu = request.getParameter("niandu");
		String baseId = pdo.getBaseId();
		pdo.setNiandu(niandu);
		int bid = 0;
		if (StringUtils.isNotEmpty(baseId)) {
			bid = Integer.parseInt(baseId);
		}
		try {
			Set<Integer> ids = baseInfoService.addProjects(pdo, bid);
			baseInfoService.updateProject(ids, bid);
			return new AjaxObj(1, "操作成功！");
		} catch (BussinessException e) {
			return new AjaxObj(0, e.getMessage());
		}
	}

	/**
	 * 列出baseId下的所有項目信息
	 * 
	 * @param request
	 * @return
	 */
/*	@RequestMapping(value = "project", method = RequestMethod.GET)
	@ResponseBody
	public ResultType listProjects(HttpServletRequest request) {
		String baseInfoId = request.getParameter("baseInfoId");
		int id = 0;
		if (StringUtils.isNotEmpty(baseInfoId)) {
			id = Integer.valueOf(baseInfoId);
		}
		ProjectDto projects = baseInfoService.getProjects(id);
		Object info[] = projects.toArray();
		ResultType r = new ResultType(info, projects.size());
		return r;
	}*/

	@RequestMapping(value = "clearproject/{baseinfoId}", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj clearProject(HttpServletRequest request,
			@PathVariable("baseinfoId") int bid) {
		String niandu = request.getParameter("niandu");
		try {
			baseInfoService.clearProject(bid, niandu);
			return new AjaxObj(1, "操作成功！");
		} catch (BussinessException e) {
			return new AjaxObj(0, e.getMessage());
		}
	}

	/**
	 * 添加和更新家庭成员信息
	 * 
	 * @param mdo
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addupdatemember", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj addMemberinfo(MemberDto mdo, HttpSession session,
			HttpServletRequest request) {
		try {
			if (Integer.parseInt(mdo.getMemberId()) > 0) {
				// 更新操作
				memberInfoService.updateMember(mdo);
				return new AjaxObj(1, "更新成功！");
			} else {// 新增操作
				MemberDto memberDto = memberInfoService.createArchive(mdo);
				memberInfoService.updateMemberInfo(memberDto.getMemberId(),
						mdo.getBaseInfoId());
				return new AjaxObj(1, "添加成功！");
			}
		} catch (BussinessException e) {
			return new AjaxObj(0, e.getMessage());
		}
	}

	/**
	 * 列出所有的该建档立卡信息下的家庭成员信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "memberinfo", method = RequestMethod.GET)
	@ResponseBody
	public ResultType listMembers(HttpServletRequest request) {
		String baseInfoId = request.getParameter("baseInfoId");
		int id = 0;
		if (StringUtils.isNotEmpty(baseInfoId)) {
			id = Integer.valueOf(baseInfoId);
		}
		List<MemberDto> listMember = memberInfoService.listMember(id);
		Object info[] = listMember.toArray();
		ResultType r = new ResultType(info, listMember.size());
		return r;
	}

	@RequestMapping(value = "deletemember/{ids}", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj deleteMember(@PathVariable("ids") String ids) {
		String[] idArr = new String[] {};
		if (StringUtils.isNotEmpty(ids)) {
			idArr = ids.split(",");
		}
		try {
			for (String sid : idArr) {
				if (StringUtils.isNotEmpty(sid)) {
					int id = Integer.valueOf(sid);
					memberInfoService.deleteMemberInfoById(id);
				}
			}
			return new AjaxObj(1, idArr.length + "条数据删除成功！");
		} catch (BussinessException e) {
			return new AjaxObj(0, "删除失败，请稍后重试！");
		}
	}

	/*
	 * @RequestMapping(value = "addbaseinfo", method = RequestMethod.POST)
	 * 
	 * @ResponseBody public AjaxObj addBaseinfo(BaseInfoDto bio, HttpSession
	 * session, HttpServletRequest request) { try { if (bio.getBaseInfoId() > 0)
	 * { // 更新操作 baseInfoService.updateBaseinfo(bio.getBaseInfo()); return new
	 * AjaxObj(1, AjaxObj.MSG_SUCCESS_UPDATE); } else { // 新增操作 FManager manager
	 * = (FManager) session.getAttribute("user"); if (manager != null) {
	 * bio.setManager(manager); }
	 * baseInfoService.createArchive(bio.getBaseInfo()); return new AjaxObj(1,
	 * AjaxObj.MSG_SUCCESS_ADD); } } catch (BussinessException e) { return new
	 * AjaxObj(0, e.getMessage()); } }
	 */
	/**
	 * 删除建档立卡信息，可以多选和单选
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "deletebaseinfo/{ids}", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj deleteBaseinfo(@PathVariable("ids") String ids) {
		String[] idArr = new String[] {};
		if (StringUtils.isNotEmpty(ids)) {
			idArr = ids.split(",");
		}
		try {
			for (String sid : idArr) {
				if (StringUtils.isNotEmpty(sid)) {
					int id = Integer.valueOf(sid);
					baseInfoService.deleteBaseInfoById(id);
				}
			}
			return new AjaxObj(1, idArr.length + "条数据删除成功！");
		} catch (BussinessException e) {
			return new AjaxObj(0, "删除失败，请稍后重试！");
		}
	}

	/**
	 * 更新建档立卡信息
	 * 
	 * @param fbi
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updatebaseinfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj updateBaseinfo(FBaseInfo fbi, HttpSession session,
			HttpServletRequest request) {
		// System.out.println(request);
		Map parameterMap = request.getParameterMap();
		Set set = parameterMap.keySet();
		System.out.println(set.size());
		try {
			if (fbi.getId() > 0) {
				// 更新操作
				baseInfoService.updateBaseinfo(fbi);
				return new AjaxObj(1, AjaxObj.MSG_SUCCESS_UPDATE, fbi.getId());
			} else {
				// 新增操作
				FManager manager = (FManager) session.getAttribute("user");
				if (manager != null) {
					fbi.setManager(manager);
				}
				FBaseInfo baseInfo = baseInfoService.createArchive(fbi);
				return new AjaxObj(1, AjaxObj.MSG_SUCCESS_ADD, baseInfo.getId());
			}
		} catch (BussinessException e) {
			return new AjaxObj(0, e.getMessage());
		}
	}

	/**
	 * 列出所有的建档立卡信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "baseinfos", method = RequestMethod.GET)
	@ResponseBody
	public ResultType listBaseinfo(HttpServletRequest request) {
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");

		String huzhuName = request.getParameter("huzhuName");
		String town = request.getParameter("town");
		String isOutpoor = request.getParameter("isOutPoor");
		String addTime = request.getParameter("addTime");

		SystemContext.setPageSize(Integer.parseInt(limit));
		SystemContext.setPageOffset(Integer.parseInt(start));
		// 登录用户
		FManager manager = (FManager) request.getSession().getAttribute("user");
		Pager<FBaseInfo> infos = baseInfoService.findBaseInfo(manager,
				huzhuName, town, isOutpoor, addTime);
		int results = (int) (infos == null ? 0 : infos.getTotal());
		Object info[] = infos.getDatas().toArray();
		ResultType r = new ResultType(info, results);
		return r;
	}
}
